package com.coolcode.controller;

import com.coolcode.anno.ParamValid;
import com.coolcode.dto.LoginReqDto;
import com.coolcode.dto.LoginRespDto;
import com.coolcode.service.LoginService;
import com.coolcode.vo.LoginReqVO;
import com.coolcode.vo.LoginRespVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


/**
 * 自定义登录接口
 */
@Slf4j
@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;


    @PostMapping("/login")
    @ParamValid
    public ResponseEntity<LoginRespVO> login(@RequestBody @Valid LoginReqVO loginReqVO,
                                             BindingResult bindingResult) {
        LoginRespDto loginRespDto = loginService.login(new LoginReqDto(loginReqVO));
        loginRespDto.setUsername(loginRespDto.getUsername());
        return new ResponseEntity<>(new LoginRespVO(loginRespDto), HttpStatus.OK);
    }

    @PostMapping("/logout")
    @ParamValid
    public ResponseEntity<String> logout() {

        loginService.logout();
        return new ResponseEntity<>("注销成功", HttpStatus.OK);
    }
}
